<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>美办集成GFS文件服务 | 尼古拉斯-鱼丸博客 | 喝酒、吃肉、不赌</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="GFS">
    <meta name="description" content="什么是GFS?GFS文件服务系统，负责文件的上传存储、访问等功能。 GFS文件存储和文件浏览流程文件存储                                                                                             文件存储             FileHelper工具类发送GFS请求123//Ribbon请求调用方式p">
<meta name="keywords" content="GFS">
<meta property="og:type" content="article">
<meta property="og:title" content="美办集成GFS文件服务">
<meta property="og:url" content="http://fish_08.oschina.io/2017/08/29/meipro-file/index.html">
<meta property="og:site_name" content="尼古拉斯-鱼丸博客">
<meta property="og:description" content="什么是GFS?GFS文件服务系统，负责文件的上传存储、访问等功能。 GFS文件存储和文件浏览流程文件存储                                                                                             文件存储             FileHelper工具类发送GFS请求123//Ribbon请求调用方式p">
<meta property="og:image" content="http://fish_08.oschina.io/img/meiprofile/upload.png">
<meta property="og:image" content="http://fish_08.oschina.io/img/meiprofile/download.png">
<meta property="og:image" content="http://fish_08.oschina.io/img/meiprofile/file.png">
<meta property="og:image" content="http://fish_08.oschina.io/img/meiprofile/idworker.png">
<meta property="og:updated_time" content="2017-08-31T06:44:31.193Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="美办集成GFS文件服务">
<meta name="twitter:description" content="什么是GFS?GFS文件服务系统，负责文件的上传存储、访问等功能。 GFS文件存储和文件浏览流程文件存储                                                                                             文件存储             FileHelper工具类发送GFS请求123//Ribbon请求调用方式p">
<meta name="twitter:image" content="http://fish_08.oschina.io/img/meiprofile/upload.png">
    
        <link rel="alternative" href="/atom.xml" title="尼古拉斯-鱼丸博客" type="application/atom+xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.6.7">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">于亮</h5>
          <a href="mailto:136214289@qq.com" title="136214289@qq.com" class="mail">136214289@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                文档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://git.oschina.net/fish_08" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://weibo.com/2837335591/profile?rightmod=1&wvr=6&mod=personinfo" target="_blank" >
                <i class="icon icon-lg icon-weibo"></i>
                Weibo
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/custom"  >
                <i class="icon icon-lg icon-link"></i>
                测试
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">美办集成GFS文件服务</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">美办集成GFS文件服务</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-08-29T06:17:38.000Z" itemprop="datePublished" class="page-time">
  2017-08-29
</time>


            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#什么是GFS"><span class="post-toc-number">1.</span> <span class="post-toc-text">什么是GFS?</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#GFS文件存储和文件浏览流程"><span class="post-toc-number">2.</span> <span class="post-toc-text">GFS文件存储和文件浏览流程</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#GFS接入方式准备"><span class="post-toc-number">3.</span> <span class="post-toc-text">GFS接入方式准备</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Meipro-File使用"><span class="post-toc-number">4.</span> <span class="post-toc-text">Meipro-File使用</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Meipro-file-jar简介"><span class="post-toc-number">4.1.</span> <span class="post-toc-text">Meipro-file.jar简介</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#meipro-file包引入"><span class="post-toc-number">4.2.</span> <span class="post-toc-text">meipro-file包引入</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#配置mongoDB和gfs相关信息"><span class="post-toc-number">4.3.</span> <span class="post-toc-text">配置mongoDB和gfs相关信息</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#文件上传和权限控制下文件浏览"><span class="post-toc-number">4.4.</span> <span class="post-toc-text">文件上传和权限控制下文件浏览</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#文件上传"><span class="post-toc-number">4.4.1.</span> <span class="post-toc-text">文件上传</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#权限控制下文件浏览"><span class="post-toc-number">4.4.2.</span> <span class="post-toc-text">权限控制下文件浏览</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#MongoDB使用场景"><span class="post-toc-number">5.</span> <span class="post-toc-text">MongoDB使用场景</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#分布式ID生成器"><span class="post-toc-number">6.</span> <span class="post-toc-text">分布式ID生成器</span></a></li></ol>
        </nav>
    </aside>
    
<article id="post-meipro-file"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">美办集成GFS文件服务</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年08月29日 14:17" datetime="2017-08-29T06:17:38.000Z"  itemprop="datePublished">2017-08-29</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h3 id="什么是GFS"><a href="#什么是GFS" class="headerlink" title="什么是GFS?"></a>什么是GFS?</h3><p>GFS文件服务系统，负责文件的上传存储、访问等功能。</p>
<h3 id="GFS文件存储和文件浏览流程"><a href="#GFS文件存储和文件浏览流程" class="headerlink" title="GFS文件存储和文件浏览流程"></a>GFS文件存储和文件浏览流程</h3><p><strong>文件存储</strong><br><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/img/meiprofile/upload.png" alt="文件存储" title="">
                </div>
                <div class="image-caption">文件存储</div>
            </figure><br><strong>FileHelper工具类发送GFS请求</strong><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">//Ribbon请求调用方式</div><div class="line">public  ReturnData postFileToGFSRequset(String customFileName,InputStream inputStream) throws MeiproRestClientException;//上传文件</div><div class="line">public  ReturnData postPictureToGFSRequset(String suffix,InputStream inputStream) throws MeiproRestClientException;//上传图片</div></pre></td></tr></table></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div></pre></td><td class="code"><pre><div class="line"> //原生http上传文件调用方式</div><div class="line"> public void uploadCustomFile() throws IOException&#123;</div><div class="line">	String url = <span class="string">"http://ip:port/api/uploadCustomFile?customFileName=unitTesCeshi.txt"</span>;</div><div class="line">	File file = new File(<span class="string">"E:/unitTesCeshi.txt"</span>);</div><div class="line">	FileInputStream fis = new FileInputStream(file);</div><div class="line">	ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);</div><div class="line">	byte[] b = new byte[1024];</div><div class="line">	int n = 0;</div><div class="line">	<span class="keyword">while</span> ((n = fis.read(b)) != -1) &#123;</div><div class="line">	bos.write(b, 0, n);</div><div class="line">	&#125;</div><div class="line">	byte[] buffer = bos.toByteArray();</div><div class="line">	DefaultHttpClient httpClient = new DefaultHttpClient();</div><div class="line">	httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, <span class="literal">false</span>));</div><div class="line">	HttpPost post = new HttpPost(url);</div><div class="line">	post.addHeader(<span class="string">"token"</span>, <span class="string">"文件token信息"</span>); </div><div class="line">	post.setEntity(new ByteArrayEntity(buffer));</div><div class="line">	HttpResponse httpResponse = httpClient.execute(post);</div><div class="line">	String content = EntityUtils.toString(httpResponse.getEntity(),Consts.UTF_8);</div><div class="line">	System.out.println(content);</div><div class="line">	fis.close();</div><div class="line">	bos.close();</div><div class="line">&#125;</div><div class="line"></div><div class="line"> //原生http上传图片调用方式</div><div class="line"> public void fileup() throws Exception &#123;</div><div class="line">        String url = <span class="string">"http://ip:port/api/fileup?suffix=jpg"</span>;</div><div class="line">        File file = new File(<span class="string">"E:/ocr/3OCR3.jpg"</span>);</div><div class="line">        FileInputStream fis = new FileInputStream(file);</div><div class="line">        ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);</div><div class="line">        byte[] b = new byte[1024];</div><div class="line">        int n = 0;</div><div class="line">        <span class="keyword">while</span> ((n = fis.read(b)) != -1) &#123;</div><div class="line">            bos.write(b, 0, n);</div><div class="line">        &#125;</div><div class="line">      byte[] buffer =bos.toByteArray();</div><div class="line">      DefaultHttpClient httpClient = new DefaultHttpClient();</div><div class="line">      httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, <span class="literal">false</span>));</div><div class="line">      HttpPost post =new HttpPost(url);</div><div class="line">      post.addHeader(<span class="string">"token"</span>,<span class="string">"图片申请token"</span>);</div><div class="line">      post.setEntity(new ByteArrayEntity(buffer));</div><div class="line">      HttpResponse httpResponse=httpClient.execute(post);</div><div class="line">      String content =EntityUtils.toString(httpResponse.getEntity());</div><div class="line">      System.out.println(content);</div><div class="line">      fis.close();</div><div class="line">      bos.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>文件请求参数</p>
<table>
<thead>
<tr>
<th style="text-align:left">名称</th>
<th style="text-align:left">类型</th>
<th style="text-align:left">是否必填</th>
<th style="text-align:left">默认值</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">customFileName</td>
<td style="text-align:left">String</td>
<td style="text-align:left">是</td>
<td style="text-align:left">自定义目录或文件名</td>
<td style="text-align:left">(自定义文件名只支持：数字、字母、点、斜杠),文件名必须带有后缀</td>
</tr>
</tbody>
</table>
<p>图片请求参数：</p>
<table>
<thead>
<tr>
<th style="text-align:left">名称</th>
<th style="text-align:left">类型</th>
<th style="text-align:left">是否必填</th>
<th style="text-align:left">默认值</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">suffix</td>
<td style="text-align:left">String</td>
<td style="text-align:left">是</td>
<td style="text-align:left"></td>
<td style="text-align:left">后缀名：要求必须上传后缀名参数，而且必须和原图后缀一致</td>
</tr>
<tr>
<td style="text-align:left">identify</td>
<td style="text-align:left">String</td>
<td style="text-align:left">否</td>
<td style="text-align:left"></td>
<td style="text-align:left">如需进行图文识别校验—必传可参考： 3.2 GFS上传图片-异步回调 -API</td>
</tr>
<tr>
<td style="text-align:left">type</td>
<td style="text-align:left">String</td>
<td style="text-align:left">否</td>
<td style="text-align:left">敏感词校验所传类别</td>
<td style="text-align:left">默认全网校验。传类别：按全网和类别同时检测。不传类别：按全网检测(类别码请查看敏感词类别码附件)</td>
</tr>
</tbody>
</table>
<p><strong>GFS响应</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">//ReturnData 上传GFS响应结果封装对象</div><div class="line">//文件响应</div><div class="line"> &#123;</div><div class="line">   <span class="string">"result"</span>:<span class="string">"Y"</span>,</div><div class="line">   <span class="string">"errorCode"</span>:<span class="string">""</span>,</div><div class="line">   <span class="string">"msg"</span>:<span class="string">"ok"</span>,</div><div class="line">   <span class="string">"gfsFileName"</span>:<span class="string">"T1qNYTBC_T1RCvBVdK"</span>,</div><div class="line">   <span class="string">"url"</span>:<span class="string">" http://ip:port/112129.html "</span></div><div class="line"> &#125;</div><div class="line">//图片响应</div><div class="line">&#123;</div><div class="line">   <span class="string">"result"</span>: <span class="string">"Y"</span>,</div><div class="line">   <span class="string">"errorCode"</span>: <span class="string">""</span>,</div><div class="line">   <span class="string">"msg"</span>: <span class="string">"ok"</span>,</div><div class="line">   <span class="string">"gfsFileName"</span>:<span class="string">"T1AzZTBbJT1RCvBVdK"</span>,</div><div class="line">   <span class="string">"url"</span>:<span class="string">"//ip:port/T1AzZTBbJT1RCvBVdK.jpg"</span>,</div><div class="line">   <span class="string">"sensitiveWord"</span>:<span class="string">""</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>返回参数</p>
<table>
<thead>
<tr>
<th style="text-align:left">名称</th>
<th style="text-align:left">类型</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">result</td>
<td style="text-align:left">String</td>
<td style="text-align:left">返回结果：Y/N (成功/失败)</td>
</tr>
<tr>
<td style="text-align:left">errorCode</td>
<td style="text-align:left">String</td>
<td style="text-align:left">响应码</td>
</tr>
<tr>
<td style="text-align:left">msg</td>
<td style="text-align:left">String</td>
<td style="text-align:left">提示信息</td>
</tr>
<tr>
<td style="text-align:left">gfsFileName</td>
<td style="text-align:left">String</td>
<td style="text-align:left">文件名称 (文件名为18位的字符串，大文件以L开头、小文件以T开头)</td>
</tr>
<tr>
<td style="text-align:left">url</td>
<td style="text-align:left">String</td>
<td style="text-align:left">文件访问地址(域名+文件名)</td>
</tr>
</tbody>
</table>
<p>token: 如果未分配过token,需向商城文件系统部申请<br>默认返回url带后缀名，如有特殊需求, 请联系商城文件系统部进行设置。<br>例如：<br>http:// gfs.atguat.net.cn/T1gzbTByJT1RCvBVdK     &lt;无后缀&gt;<br>http:// gfs.atguat.net.cn/T1gzbTByJT1RCvBVdK.jpg  &lt;有后缀&gt;</p>
<p>响应码定义</p>
<table>
<thead>
<tr>
<th style="text-align:left">描述</th>
<th style="text-align:left">响应码</th>
<th style="text-align:left">备注说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">上传成功</td>
<td style="text-align:left">errorCode :</td>
<td style="text-align:left">msg: ok</td>
</tr>
<tr>
<td style="text-align:left">上传失败</td>
<td style="text-align:left">errorCode :20005</td>
<td style="text-align:left">msg: fileup fail</td>
</tr>
<tr>
<td style="text-align:left">缺少token</td>
<td style="text-align:left">errorCode :20001</td>
<td style="text-align:left">msg: no token</td>
</tr>
<tr>
<td style="text-align:left">token不正确</td>
<td style="text-align:left">errorCode :20002</td>
<td style="text-align:left">msg: illegal token</td>
</tr>
<tr>
<td style="text-align:left">后缀名不正确</td>
<td style="text-align:left">errorCode :20009</td>
<td style="text-align:left">msg: illegal suffix</td>
</tr>
<tr>
<td style="text-align:left">无后缀</td>
<td style="text-align:left">errorCode :20010</td>
<td style="text-align:left">msg: no suffix</td>
</tr>
<tr>
<td style="text-align:left">文件含有敏感词汇</td>
<td style="text-align:left">errorCode :20011</td>
<td style="text-align:left">msg: contains sensitive words</td>
</tr>
<tr>
<td style="text-align:left">上传文件非图片类型</td>
<td style="text-align:left">errorCode :20012</td>
<td style="text-align:left">msg: upload file is not pictures</td>
</tr>
<tr>
<td style="text-align:left">文件过大</td>
<td style="text-align:left">errorCode :20018</td>
<td style="text-align:left">msg:img exceed the maximum limit</td>
</tr>
<tr>
<td style="text-align:left">缺少文件名</td>
<td style="text-align:left">errorCode :20014</td>
<td style="text-align:left">no filename</td>
</tr>
<tr>
<td style="text-align:left">文件名不合法</td>
<td style="text-align:left">errorCode :20003</td>
<td style="text-align:left">illegal filename</td>
</tr>
<tr>
<td style="text-align:left">文件名已存在，请重命名</td>
<td style="text-align:left">errorCode :20015</td>
<td style="text-align:left">The file name is exists, please redefine</td>
</tr>
<tr>
<td style="text-align:left">插入自定义文件失败</td>
<td style="text-align:left">errorCode :20016</td>
<td style="text-align:left">msg:img exceed the maximum limit</td>
</tr>
<tr>
<td style="text-align:left">文件名格式不能和tfs文件名格式一致</td>
<td style="text-align:left">errorCode :20017</td>
<td style="text-align:left">and TFS file name format same file name not allowed</td>
</tr>
<tr>
<td style="text-align:left">更新文件失败</td>
<td style="text-align:left">errorCode :20020</td>
<td style="text-align:left">update file fail</td>
</tr>
<tr>
<td style="text-align:left">查询接入方信息失败</td>
<td style="text-align:left">errorCode :20021</td>
<td style="text-align:left">query accessPartyInfo fail</td>
</tr>
<tr>
<td style="text-align:left">更新接口未启用</td>
<td style="text-align:left">errorCode :20022</td>
<td style="text-align:left">update interface no enabled</td>
</tr>
</tbody>
</table>
<p><strong>文件浏览</strong><br><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/img/meiprofile/download.png" alt="文件浏览" title="">
                </div>
                <div class="image-caption">文件浏览</div>
            </figure></p>
<h3 id="GFS接入方式准备"><a href="#GFS接入方式准备" class="headerlink" title="GFS接入方式准备"></a>GFS接入方式准备</h3><p>1、请填写附件‘接入方信息模板’<br>2、发邮件到 scwjxt@gomeplus.com 申请上传凭证信息token<br>模板传送门：<a href="http://wiki.ds.gome.com.cn/pages/viewpage.action?pageId=11797311" target="_blank" rel="external">http://wiki.ds.gome.com.cn/pages/viewpage.action?pageId=11797311</a></p>
<h3 id="Meipro-File使用"><a href="#Meipro-File使用" class="headerlink" title="Meipro-File使用"></a>Meipro-File使用</h3><h4 id="Meipro-file-jar简介"><a href="#Meipro-file-jar简介" class="headerlink" title="Meipro-file.jar简介"></a>Meipro-file.jar简介</h4><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/img/meiprofile/file.png" alt="file简介" title="">
                </div>
                <div class="image-caption">file简介</div>
            </figure>
<p><strong>FileHelper</strong>:可以直接注入，负责发送图片、文件到GFS文件服务，从GFS获取文件字节数组<br><strong>ReturnData</strong>: 图片、文件上传到GFS文件服务响应对象<br><strong>MeiproFileException</strong>:异常处理,继承MeiproException,处理GFS错误响应码<br><strong>MeiproFileMongoRepository</strong>:负责MeiproFileMongoDB到mongoDB的CRUD操作<br><strong>MeiproFileMongoDB</strong>:文件信息封装对象<br><strong>SpringBeanUtil</strong>:Spring的ApplicationContext上下文获取工具类，用户获取自定义拦截器获取bean<br><strong>IdWorker</strong>:分布式id生成器<br><strong>GfsCustomYmlUtil</strong>:读取配置中心custom包下对应应用配置gfs地址和token信息</p>
<h4 id="meipro-file包引入"><a href="#meipro-file包引入" class="headerlink" title="meipro-file包引入"></a>meipro-file包引入</h4><pre><code class="bash">&lt;dependency&gt;
    &lt;groupId&gt;com.gomeplus&lt;/groupId&gt;
    &lt;artifactId&gt;meipro-file&lt;/artifactId&gt;
    &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>
<h4 id="配置mongoDB和gfs相关信息"><a href="#配置mongoDB和gfs相关信息" class="headerlink" title="配置mongoDB和gfs相关信息"></a>配置mongoDB和gfs相关信息</h4><p>a.修改bootstrap.yml </p>
<pre><code class="bash">spring:
  application:
    name: appstore-microservice
  profiles:
    active: dev
    include: mysql,mongo
  cloud:
    config:
      uri: <span class="variable">${CONFIG_SERVER_URI:http://........:9999/configServer}</span>
      fail-fast: <span class="literal">true</span>
</code></pre>
<p>b.修改meipro-config-repo项目master分支custom文件夹中对应应用文件配置</p>
<pre><code class="bash">gfs:
    prop:
        gfsPictureUrl: http://ip:port/api/fileup?suffix=
        pictureToken: 670a34c91d024e298b4rewrewrewrewrewrw
        pictureSize: 10485760
        gfsFileUrl: 
        fileToken: 
        fileSize:
</code></pre>
<h4 id="文件上传和权限控制下文件浏览"><a href="#文件上传和权限控制下文件浏览" class="headerlink" title="文件上传和权限控制下文件浏览"></a>文件上传和权限控制下文件浏览</h4><h5 id="文件上传"><a href="#文件上传" class="headerlink" title="文件上传"></a>文件上传</h5><pre><code class="bash"> @Autowired
 FileHelper fileHelper
 @Autowired
 MeiproFileMongoRepository meiproFileMongoRepository;
 @Autowired
 GfsCustomYmlUtil  gfsCustomYmlUtil;

 ReturnData returnData=fileHelper.postPictureToGFSRequset(suffixName,inputStream);
 MeiproFileMongoDB meiproFileMongoDB=new MeiproFileMongoDB();
 meiproFileMongoDB.setAppType(<span class="string">"operation"</span>);
 meiproFileMongoDB.setCtime(new Date().getTime());
 meiproFileMongoDB.setFileName(fileOriginalFilename);
 meiproFileMongoDB.setOwnerName(ownerName);
 meiproFileMongoDB.setContentType(file.getContentType());
 meiproFileMongoDB.setUrl(returnData.getUrl());
meiproFileMongoRepository.insert(meiproFileMongoDB);
</code></pre>
<h5 id="权限控制下文件浏览"><a href="#权限控制下文件浏览" class="headerlink" title="权限控制下文件浏览"></a>权限控制下文件浏览</h5><p><strong>1.配置文件访问控制Filter</strong></p>
<pre><code class="bash">//自定义filterurl过滤规则配置
@Configuration
public class AppstoreShiroConfigure  extends BaseShiroConfiguration {

   @Override
   protected void loadShiroFilterChain(ShiroFilterFactoryBean shiroFilterFactoryBean) {
       Map&lt;String, String&gt; filterChainDefinitionMap = new LinkedHashMap&lt;String, String&gt;();
       filterChainDefinitionMap.put(<span class="string">"/a/**"</span>, <span class="string">"anon"</span>); //不需要登录
       filterChainDefinitionMap.put(<span class="string">"/b/**"</span>, <span class="string">"userAuth,roleAuth[a]"</span>);
       filterChainDefinitionMap.put(<span class="string">"/c/**"</span>, <span class="string">"userAuth"</span>);
       shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
   }

   @Override
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
       ShiroFilterFactoryBean shiroFilterFactoryBean=super.getShiroFilterFactoryBean(securityManager) ;
       shiroFilterFactoryBean.getFilters().put(<span class="string">"fileAuth"</span>, new AppstoreAuthFilter());
       Map&lt;String, String&gt; filterChainDefinitionMap =shiroFilterFactoryBean.getFilterChainDefinitionMap();
       filterChainDefinitionMap.put(<span class="string">"/d/file/**"</span>, <span class="string">"fileAuth"</span>);
       shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
       <span class="built_in">return</span> shiroFilterFactoryBean;
    }

}
</code></pre>
<p><strong>2.实现自定义filter</strong></p>
<pre><code class="bash">//自定义filter
@Component
public class AppstoreAuthFilter extends AccessControlFilter {

    //此方法为是否允许访问控制，此处实现权限检测逻辑，返回是否允许访问标识<span class="literal">true</span> <span class="literal">false</span>
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        MeiproFileMongoRepository m=(MeiproFileMongoRepository)SpringBeanUtil.getBean(<span class="string">"meiproFileMongoRepository"</span>);
        MeiproFileMongoDB mb=m.findOne(<span class="string">"599e85f34c7034197xxxxxx"</span>);
        SpringBeanUtil.getBean(<span class="string">"meiproFileMongoRepository"</span>);
        String appType=mb.getAppType();
        <span class="keyword">if</span>(appType==null){
            <span class="built_in">return</span> <span class="literal">false</span>;
        }
        <span class="built_in">return</span>  <span class="literal">true</span>;
    }

    //此在拒绝访问后执行，继续处理还是直接返回没有权限json串
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        ResponseJson json = new ResponseJson(StatusCode.NO_PERMISSION);
        sendResponse((HttpServletResponse) response, json);
        <span class="built_in">return</span> <span class="literal">false</span>;
    }

    private void sendResponse(HttpServletResponse response, ResponseJson responseJson) throws IOException {
        response.getWriter().write(JSON.toJSONString(responseJson));
    }

}
</code></pre>
<p><strong>3.正常文件浏览</strong></p>
<pre><code class="bash">@Autowired
FileHelper  fileHelper;

byte[] inputStream=fileHelper.getFileByteArray(<span class="string">"http://ip:port/T1UFdTBCL_1RCvBVdK.jpg"</span>);
</code></pre>
<h3 id="MongoDB使用场景"><a href="#MongoDB使用场景" class="headerlink" title="MongoDB使用场景"></a>MongoDB使用场景</h3><p>1.数据库需要横向扩展能力的，用来存储数据规模会在未来非常巨大的数据，如日志、评论等等。</p>
<p>2.对于数据事务性要求不高的操作，MongoDB 目前只支持单文档事务，需要复杂事务支持的场景暂时不适合。</p>
<p>从目前阿里云 MongoDB 云数据库上的用户看，MongoDB 的应用已经渗透到各个领域，比如游戏、物流、电商、内容管理、社交、物联网、视频直播等，以下是几个实际的应用案例。       </p>
<table>
<thead>
<tr>
<th style="text-align:left">应用场景</th>
<th style="text-align:left">应用案例</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">游戏场景</td>
<td style="text-align:left">使用 MongoDB 存储游戏用户信息，用户的装备、积分等直接以内嵌文档的形式存储，方便查询、更新</td>
</tr>
<tr>
<td style="text-align:left">物流场景</td>
<td style="text-align:left">使用 MongoDB 存储订单信息，订单状态在运送过程中会不断更新，以 MongoDB 内嵌数组的形式来存储，一次查询就能将订单所有的变更读取出来。</td>
</tr>
<tr>
<td style="text-align:left">社交场景</td>
<td style="text-align:left">使用 MongoDB 存储存储用户信息，以及用户发表的朋友圈信息，通过地理位置索引实现附近的人、地点等功能</td>
</tr>
<tr>
<td style="text-align:left">物联网场景</td>
<td style="text-align:left">使用 MongoDB 存储所有接入的智能设备信息，以及设备汇报的日志信息，并对这些信息进行多维度的分析</td>
</tr>
<tr>
<td style="text-align:left">视频直播</td>
<td style="text-align:left">使用 MongoDB 存储用户信息、礼物信息等……</td>
</tr>
<tr>
<td style="text-align:left">…..</td>
<td style="text-align:left">?</td>
</tr>
</tbody>
</table>
<p>如果你还在为是否应该使用 MongoDB，不如来做几个选择题来辅助决策（注：以下内容改编自 MongoDB 公司 TJ 同学的某次公开技术分享）。                                                </p>
<table>
<thead>
<tr>
<th style="text-align:left">应用特征</th>
<th style="text-align:left">Yes / No</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">应用不需要事务及复杂 join 支持</td>
<td style="text-align:left">必须 Yes</td>
</tr>
<tr>
<td style="text-align:left">新应用，需求会变，数据模型无法确定，想快速迭代开发</td>
<td style="text-align:left">？</td>
</tr>
<tr>
<td style="text-align:left">应用需要2000-3000以上的读写QPS（更高也可以）</td>
<td style="text-align:left">？</td>
</tr>
<tr>
<td style="text-align:left">应用需要TB甚至 PB 级别数据存储</td>
<td style="text-align:left">?</td>
</tr>
<tr>
<td style="text-align:left">应用发展迅速，需要能快速水平扩展</td>
<td style="text-align:left">?</td>
</tr>
<tr>
<td style="text-align:left">应用要求存储的数据不丢失</td>
<td style="text-align:left">?</td>
</tr>
<tr>
<td style="text-align:left">应用需要99.999%高可用</td>
<td style="text-align:left">?</td>
</tr>
<tr>
<td style="text-align:left">应用需要大量的地理位置查询、文本查询</td>
<td style="text-align:left">？</td>
</tr>
</tbody>
</table>
<p>如果上述有1个 Yes，可以考虑 MongoDB，2个及以上的 Yes，选择MongoDB绝不会后悔。</p>
<h3 id="分布式ID生成器"><a href="#分布式ID生成器" class="headerlink" title="分布式ID生成器"></a>分布式ID生成器</h3><p> idworker简单来说就是一个分布式高效有序ID生产黑科技工具，思路主要是来源于Twitter-Snowflake算法。<br><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/img/meiprofile/idworker.png" alt="file简介" title="">
                </div>
                <div class="image-caption">file简介</div>
            </figure></p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-08-31T06:44:31.193Z" itemprop="dateUpdated">2017年8月31日 14:44</time>
</span><br>


        如要转载请注明出处：<a href="/2017/08/29/meipro-file/" target="_blank" rel="external">http://fish_08.oschina.io/2017/08/29/meipro-file/</a>
    </div>
    <footer>
        <a href="http://fish_08.oschina.io">
            <img src="/img/avatar.jpg" alt="于亮">
            于亮
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/GFS/">GFS</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://fish_08.oschina.io/2017/08/29/meipro-file/&title=《美办集成GFS文件服务》 — 尼古拉斯-鱼丸博客&pic=http://fish_08.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://fish_08.oschina.io/2017/08/29/meipro-file/&title=《美办集成GFS文件服务》 — 尼古拉斯-鱼丸博客&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://fish_08.oschina.io/2017/08/29/meipro-file/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《美办集成GFS文件服务》 — 尼古拉斯-鱼丸博客&url=http://fish_08.oschina.io/2017/08/29/meipro-file/&via=http://fish_08.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://fish_08.oschina.io/2017/08/29/meipro-file/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/11/29/大型网站技术架构1-网站高性能/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">大型网站技术架构1-网站高性能</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/08/17/activity/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">Activiti</h4>
      </a>
    </div>
  
</nav>



    







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢包子~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.png" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.png" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="http://fish_08.oschina.io/" target="_blank">yuliang</a></span>
            <span>尼古拉斯-鱼丸博客 &copy; 2015 - 2018</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://fish_08.oschina.io/2017/08/29/meipro-file/&title=《美办集成GFS文件服务》 — 尼古拉斯-鱼丸博客&pic=http://fish_08.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://fish_08.oschina.io/2017/08/29/meipro-file/&title=《美办集成GFS文件服务》 — 尼古拉斯-鱼丸博客&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://fish_08.oschina.io/2017/08/29/meipro-file/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《美办集成GFS文件服务》 — 尼古拉斯-鱼丸博客&url=http://fish_08.oschina.io/2017/08/29/meipro-file/&via=http://fish_08.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://fish_08.oschina.io/2017/08/29/meipro-file/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



</script>

<script src="/js/main.min.js?v=1.6.7"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.6.7" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
